package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import com.alibaba.citrus.cr.platform.enhance.repository.RoleRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.BaselineGetRoleRequest;
import com.epoch.app.bcorder.model.dto.BaselineGetRolesRequest;
import com.epoch.app.bcorder.model.dto.RolePermissionSDO;
import com.epoch.app.bcorder.model.dto.RoleSDO;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.crplatformenhance.model.dto.QueryRoleRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/12/1 15:05
 */
@Component
public class RoleRepositoryImpl implements RoleRepository {

    @Resource
    private BaseDataService baseDataService;

    @Override
    public Result<String> createRole(RoleSDO sdo) {
        Result<String> result = baseDataService.baselineCreateRole(sdo);
        if(!result.isSuccess()){
            return Result.fail("",result.getMessage());
        }
        return result;
    }

    @Override
    public Result<Boolean> updateRole(RoleSDO sdo) {
        Result<Boolean> result = baseDataService.baselineUpdateRole(sdo);
        if(!result.isSuccess()){
            return Result.fail("",result.getMessage());
        }
        return result;
    }

    @Override
    public Result<Boolean> removeRole(RoleSDO sdo) {
        Result<Boolean> result = baseDataService.baselineRemoveRole(sdo);
        if(!result.isSuccess()){
            return Result.fail("",result.getMessage());
        }
        return result;
    }

    @Override
    public List<RoleSDO> queryRoleList(QueryRoleRequest request) {
        if(request == null){
            request = new QueryRoleRequest();
        }
        BaselineGetRolesRequest queryRequest = getRolesRequestConverter(request);
        Result<List<RoleSDO>> result = baseDataService.baselineGetRoles(queryRequest);
        return result.getResult();
    }

    @Override
    public RolePermissionSDO queryRoleInfo(QueryRoleRequest request) {
        if(request == null){
            request = new QueryRoleRequest();
        }
        BaselineGetRoleRequest queryRequest = getRoleRequestConverter(request);
        Result<RolePermissionSDO> result = baseDataService.baselineGetRole(queryRequest);
        return result.getResult();
    }

    private BaselineGetRolesRequest getRolesRequestConverter(QueryRoleRequest request){
        BaselineGetRolesRequest baselineGetRolesRequest = BaselineGetRolesRequest.builder()
                .id(request.getId())
                .name(request.getName())
                .code(request.getCode())
                .type(request.getType())
                .belongOrganizationId(request.getBelongOrganizationId())
                .belongOrganizationIds(request.getBelongOrganizationIds())
                .needPermission(request.getNeedPermission())
                .group(request.getGroup())
                .build();
        return baselineGetRolesRequest;
    }

    private BaselineGetRoleRequest getRoleRequestConverter(QueryRoleRequest request){
        BaselineGetRoleRequest baselineGetRolesRequest = BaselineGetRoleRequest.builder()
                .roleId(request.getId())
                .code(request.getCode())
                .belongOrganizationId(request.getBelongOrganizationId())
                .build();
        return baselineGetRolesRequest;
    }


}
